package io.gitee.loulan_yxq.owner.json.test;

import io.gitee.loulan_yxq.owner.core.bean.BeanTool;
import io.gitee.loulan_yxq.owner.core.collection.CollTool;
import io.gitee.loulan_yxq.owner.json.bean.JsonBeanTool;
import io.gitee.loulan_yxq.owner.json.test.domain.User;
import io.gitee.loulan_yxq.owner.json.test.domain.User2;
import io.gitee.loulan_yxq.owner.json.test.domain.UserDate;
import io.gitee.loulan_yxq.owner.json.tool.JsonTool;
import org.apache.commons.beanutils.converters.DateConverter;
import org.junit.Before;
import org.junit.Test;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*********************************************************
 ** TODO 
 ** <br><br>
 ** Date: Created in 2022/1/20  18:21
 ** @author loulan
 ** @version 0.0.0
 *********************************************************/
public class JsonBeanToolTest2 {

    private User user;
    private List<User> userList;
    private User2 user2;
    private List<User2> user2List;
    private UserDate userDate;

    @Before
    public void init () {
        this.user = new User();
        user.setSex("未知");
        user.setName("xxoo");
        user.setAge(10);
        user.setBirthday(new Date());

        this.userList = CollTool.list();
        for (int i = 0; i < 15; i++) {
            User userTemp = new User();
            userTemp.setSex("未知");
            userTemp.setName("xxoo");
            userTemp.setAge(i + 12);
            userTemp.setBirthday(new Date());
            this.userList.add(userTemp);
        }

        this.user2 = new User2();
        user2.setSex("未知");
        user2.setName("xxoo");
        user2.setAge(10);
        user2.setList(userList);

        this.user2List = CollTool.list();
        for (int i = 0; i < 15; i++) {
            User2 userTemp = new User2();
            userTemp.setSex("未知");
            userTemp.setName("xxoo");
            userTemp.setAge(i + 12);
            userTemp.setList(userList);
            this.user2List.add(userTemp);
        }

        this.userDate = new UserDate();
        userDate.setUsername("杨晓强");
        userDate.setAge(12);
        userDate.setBirthday(new Date());
        userDate.setBirthday1(LocalTime.now());
        userDate.setBirthday2(LocalDate.now());
        userDate.setBirthday3(LocalDateTime.now());
    }


    @Test
    public void mapToBean() {
        Map<String, Object> map = JsonTool.parseMap(JsonTool.toJsonBytes(userDate));

        // 这个执行主要是初始化JsonTool
        // fixme 如果下面这个连接可以的话，可以让BeanTool的mapToBean方法继续使用
        // https://blog.csdn.net/doctor_who2004/article/details/41292195
        BeanTool.copy(user2, User.class);
        JsonTool.toJson(user);
        long l1 = System.currentTimeMillis();
        UserDate dataJ = JsonBeanTool.mapToBean(map, UserDate.class);
        long l2 = System.currentTimeMillis();
        UserDate dataD = BeanTool.mapToBean(map, UserDate.class);
        long l3 = System.currentTimeMillis();

        System.out.println(dataJ);
        System.out.println(dataD);
        System.out.println("通过json方式的时间：  " + (l2 - l1));
        System.out.println("通过BeanTool方式的时间：  " + (l3 - l2));
    }
}
